package com.SocketDemo.TCPtest.test4;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

/**
 * @Author milian
 * @Date 2021/8/2 0002 17:54
 */
public class ThreadSocket implements Runnable{
    private Socket accept;
    public ThreadSocket(Socket accept) {
       this.accept=accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bufferedOutputStream = null;
        try {
            BufferedInputStream bufferedInputStream = new BufferedInputStream(accept.getInputStream());
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("H:\\javaDemo\\src\\com\\SocketDemo\\TCPtest\\test2\\"+ UUID.randomUUID()+".jpg"));
            int b;
            while ((b=bufferedInputStream.read())!=-1){
                bufferedOutputStream.write(b);
            }
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bufferedWriter.write("上传成功");
            bufferedWriter.newLine();
            bufferedWriter.flush();



        } catch (IOException e) {
            e.printStackTrace();
        } finally {
           if (bufferedOutputStream!=null){
               try {
                   bufferedOutputStream.close();
               } catch (IOException e) {
                   e.printStackTrace();
               }
           }
            if (accept!=null){
                try {
                    accept.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
